package cn.gailvlun.gll.util

import android.graphics.Bitmap
import android.os.Environment
import com.blankj.utilcode.util.FileUtils
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream


object FileUtil {
    private val ROOT_DIR = "${Environment.getExternalStorageDirectory().absolutePath}${File.separator}gailvlun"
    val IMAGE_CACHE_DIR: String
        get() {
            return "$ROOT_DIR${File.separator}imageCache"
        }
    val IMAGES_DIR: String
        get() {
            return "$ROOT_DIR${File.separator}images"
        }











    init {
        FileUtils.createOrExistsDir(IMAGES_DIR)
        FileUtils.createOrExistsDir(IMAGE_CACHE_DIR)
    }


    data class FileInfo(var isSuccessful: Boolean, var file: File?)

    fun saveBitmapToFile(fileName: String, bitmap: Bitmap): FileInfo {
        return saveBitmapToFile(IMAGE_CACHE_DIR, fileName, bitmap)
    }

    fun saveBitmapToFile(base: String, fileName: String, bitmap: Bitmap): FileInfo {

        var path = "$base${File.separator}$fileName"
        if (FileUtils.createOrExistsFile(path)) {
            val file = FileUtils.getFileByPath(path)
            val fout: FileOutputStream
            try {
                fout = FileOutputStream(file)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout)
                fout.flush()
                fout.close()
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            } finally {
            }

            return FileInfo(true, file)
        }
        return FileInfo(false, null)
    }

    fun deleteFile(fileName: String) {
    }
}